$(document).ready(function(){
    $(".register_tab").click(function(){
        $(".register_tab").removeClass("register_labcur").addClass("register_lab");
        var obj = $(this);
        obj.addClass("register_labcur");
        if(obj.attr("id")=="personalTab"){
            if($("#regType").val()=="2"){
                window.location="./toRegister";
            }
        }else{
            if($("#regType").val()=="1"){
                window.location="./toRegisterCompany";
            }
        }
    });
    $("#personalForm").validate({
        errorElement: "div",
        rules: {
            realName:{required:false},
            mobile: {
                required:true,

            },
            mobileCode: "required",
            passwd: "required",
            passwdConfirm: {
                required:true,
                equalTo:"#passwd"
            },
            code: "required",
            agree:"required"
        },
        messages: {
            mobile: "手机号码错误，请重新输入！",
            passwd: "密码错误，请重新输入！",
            passwdConfirm:{
                required:"密码错误，请重新输入！",
                equalTo:"两次输入的密码不一致！"
            } ,
            code: "验证码错误，请重新输入！",
            agree:"您是否同意《怡天网用户协议》？"
        }
    });

    $("#registerBtn").click(function(){
        if(!$("#personalForm").valid()) return false;
        $("#personalForm").ajaxSubmit({
            dataType:"json",
            success:function(e){
                if(e.errno == "1"){
                    alert(e.errdesc);
                    $("#code").focus();
                    $("#randomCode").click();
                }else{
                    alert("注册成功！");
                    window.location="./toLogin"
                }
            }
        });
    });
    $("#cRegisterBtn").click(function(){
        if(!$("#companyForm").valid()) return false;
        $("#companyForm").ajaxSubmit({
            dataType:"json",
            success:function(e){
                if(e.errno == "1"){
                    alert(e.errdesc);
                    $("#code").focus();
                    $("#randomCode").click();
                }else{
                    alert("注册成功！");
                    window.location="./toLogin"
                }
            }
        });
    });
    $("#mobileCodeBtn").click(function(){
        toSendCode();
    });

    function toSendCode(){
        var mobile = $("#mobile").val();
        //alert(mobile);
        if (!(/^0?1[3|4|5|8][0-9]\d{8}$/.test(mobile))) {
            alert("手机号错误!")
            return;
        }
        $.post("./checkMobile", {mobile:mobile}, function(data){
            if(data.errno == "0"){
                sendCode(mobile);
            }else{
                alert("手机号已经注册，请直接登录!")
            }
        });
    }
    function sendCode(mobile){
        $.post("./sendMobileCode",{mobile:mobile}, function(msg){
            if(msg.errno == "0"){
                alert("验证码已发送！")
                timeCount(document.getElementById("mobileCodeBtn"));
            }else{
                alert(msg.errdesc);
            }
        })
    }
});